#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2018-02-07 12:24:03

import subprocess
import sys
import argparse
import os


def runSubprocess(cmd):
    subprocess.call(cmd, stdin=sys.stdin,
                    stdout=sys.stdout, stderr=sys.stdout, shell=True)


def colorPrint(color, print_string):
    color_dict = {
        'blue': '34',
        'red': '31',
        'green': '32',
        'yellow': '33',
        'pink': '35',
        'light-blue': '36'
    }

    if color in color_dict:
        print('\033[1;%sm%s\033[0m' % (color_dict[color], print_string))
    elif color in color_dict.values():
        print('\033[1;%sm%s\033[0m' % (color, print_string))
    else:
        print('颜色不支持')


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-m', action='store', dest='mail', help='开服计划邮件')
    parser.add_argument('-t', action='store',
                        dest='kaifu_time', help='开服时间是本周还是下周')
    parser.add_argument('-n', action='store', dest='num', help='开服个数')
    parser.add_argument('-type', action='store', dest='type', help='开服计划邮件')
    parser.add_argument('-a', action='store', dest='action', help='执行操作')
    parser.add_argument('-r', action='store', dest='range', help='明日开服id,逗号分隔')

    usage = '''
        生成开服计划:python tx_kaifu.py -m 开服邮件 -t now/next -a kaifuplan -n 开服个数
        生成区服名称:python tx_kaifu.py -m 开服邮件 -t now/next -a modname [-n 开服个数]
        快速选择区服:python tx_kaifu.py -type 区服类型 -a getserver -n 开服个数
        生成维护列表:python tx_kaifu.py -a weihu -r 明日开服id,以逗号分隔
        查看剩余可搭区服:python tx_kaifu.py -a count -type 区服类型/all
    '''

    script_path = '/data/script/tx_kaifu'

    action_list = {
        'kaifuplan': 'tx_kaifu_plan.sh',
        'modname': 'tx_tencent_name.sh',
        'getserver': 'txgetserver-wang.sh',
        'weihu': 'tx_weihu-wang.sh',
        'count': 'tx-count.sh',
        # 'merge': 'tx-merge-mail.sh'
    }

    opt = parser.parse_args(sys.argv[1:])
    if opt.mail is None:
        # 快速选服
        # if opt.action is None or opt.type is None or opt.num is None:
        if opt.action is None:
            colorPrint('green', usage)
            sys.exit(1)
        else:
            if opt.action == 'getserver' and opt.type is not None and opt.num is not None:
                cmd = 'bash %s/%s %s %s' % (
                    script_path, action_list[opt.action], opt.type, opt.num)
            elif opt.action == 'weihu' and opt.range is not None:
                cmd = 'bash %s/%s %s' % (script_path,
                                         action_list[opt.action], opt.range)
            elif opt.action == 'count' and opt.type is not None:
                cmd = 'bash %s/%s %s' % (script_path,
                                         action_list[opt.action], opt.type)
            else:
                colorPrint('red', '%s是不支持的操作,支持的操作为 %s' %
                           (opt.action, str(action_list.keys())))
                sys.exit(1)
            runSubprocess(cmd)
    elif os.path.isfile(opt.mail):
        # 获取区服名称和列出开服计划
        if opt.action is None or opt.kaifu_time is None or opt.num is None:
            colorPrint('green', usage)
            sys.exit(1)
        else:
            if opt.action in action_list:
                if opt.action == 'kaifuplan':
                    # 开服计划
                    cmd = 'bash %s/%s %s %s %s' % (
                        script_path, action_list[opt.action],
                        opt.kaifu_time, opt.mail, opt.num)
                    # runSubprocess(cmd)
                else:
                    # 区服名称
                    if opt.kaifu_time == 'now':
                        cmd = 'bash %s/%s %s' % (script_path,
                                                 action_list[opt.action],
                                                 opt.mail)
                    else:
                        cmd = 'bash %s/%s %s %s' % (script_path,
                                                    action_list[opt.action],
                                                    opt.mail, opt.num)
                runSubprocess(cmd)
            else:
                colorPrint('red', '%s是不支持的操作,支持的操作为 %s' %
                           (opt.action, str(action_list.keys())))
                sys.exit(1)
    else:
        colorPrint('red', '%s 邮件不存在' % opt.mail)
        sys.exit(1)
